package com.zmn.plat.admin.controller.system.tool.redis;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.zmn.common.utils.string.StringUtil;

/**
 * @author nowind
 * @since 2018/07/07 11:26
 */
@Controller
@RequestMapping("/system/tools/redis/")
public class RedisModifyController extends RedisBaseController {
    private Logger logger = LoggerFactory.getLogger(RedisModifyController.class);

    // region 修改Redis数据
    @RequestMapping(value = "addEdit")
    public ModelAndView addEdit(HttpServletRequest request, @RequestParam(value = "redisKey", defaultValue = "") String key,
        @RequestParam(value = "redisValue", defaultValue = "") String value,
        @RequestParam(value = "redisTTL", defaultValue = "0") int ttl) {

        String lable = "修改redis数据";

        if (StringUtil.isBlank(key)) {
            return ajaxDoneError("请指定 key");
        }

        setRequestAttributes(request, 135, key, 0);

        try {

            redisManager.setex(key, value, ttl);

            return ajaxDoneSuccess(String.format("%s成功！", lable));
            // return ajaxDoneSuccessAndUrl(String.format("%s成功！", lable), "/operate/tools/redis/addEdit.action?redisKey=" + key);

        } catch (Exception ex) {

            logger.error(String.format("[zmn]%s失败,key:[%s], value:[%s]", lable, key, value));
            return ajaxDoneError(String.format("%s失败！", lable));
        }

    }
    // endregion
}
